<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends CommonController 
{
    //role 角色管理 显示
    public function index()
    {
      //个数
      $count = M('auth_group')->count();
      $this->assign('num',$count);
      
        $rule = M('auth_rule')->select();
        $this->assign('rule',$rule);

        $group = M('auth_group')->select();
        $this->assign('group',$group);

        
        $this->display();
    }

    //添加显示
    public function roleadd()
    {
        $cate = M('auth_cate')->select();
        $this->assign('cate',$cate);
        $rule = M('auth_rule')->select();
        $this->assign('rule',$rule);


    	$this->display();
    }
    //add
    public function add()
    {
    	//Array ( [name] => aa [ids] => Array ( [0] => 2 ) [desc] => aaaaaaaa )
    	//print_r($_POST);
        if (IS_POST) {
            $data['title'] = I('post.name');
            if (empty(I('post.name'))) {
                $arr = array(
                    'error' =>0,
                    'info'=>"角色名不能为空"
                    );
                echo json_encode($arr);
                exit;
            }
            $arr = I('post.ids');
            $str = implode(",", $arr);
            $data['rules'] = $str;
            $data['desc'] = I('post.desc');
            $data['create_time'] = time();

           $res = M('auth_group')->add($data);
           if ($res) {
            $arr = array(
                    'error' =>1,
                    'info'=>"角色添加成功"
                    );
            echo json_encode($arr);
           }else {
            $arr = array(
                    'error' =>0,
                    'info'=>"角色添加失败"
                    );
            echo json_encode($arr);
           }
        }

    }


    //编辑显示
    public function roleedit()
    {
        $id = I('get.id');
        $sql = 'select * from auth_group where id='.$id;
        $groupinfo = M()->query($sql);
        $groupinfo = current($groupinfo);
        $this->assign('groupinfo',$groupinfo);
        //规则转数组
        $rules = explode(',', $groupinfo['rules']);
        $this->assign('rules',$rules);


        $cate = M('auth_cate')->select();
        $this->assign('cate',$cate);
        $rule = M('auth_rule')->select();
        $this->assign('rule',$rule); 



        $this->display();
    }

    //编辑
    public function edit()
    {
        //print_r($_POST);
        if (IS_POST) {
            $id = I('post.groupid');
            //验证用户组名
            $data_row = M('auth_group')->where(array('id'=>$id))->select();
            $data_row = current($data_row);

            $groupinfo = M('auth_group')->select();
            foreach ($groupinfo as $v) {
                //用户组名 title
                $data['title'] = I('post.name');
                if ($v["title"]!=$data_row['title'] && $v['title']==$data['title']) {
                  $arr = array(
                  "error" => 0,
                  "info" => "用户组已存在"
                  );
                  echo json_encode($arr);
                  exit;
                }
            }
            //入库
            $data['desc'] = I('post.desc');
            $arr = I('post.ids');
            $str = implode(",", $arr);
            $data['rules'] = $str;
            $data['edit_time'] = time();

            $res = M('auth_group')->where(array('id'=>$id))->save($data);
           if ($res) {
            $arr = array(
                    'error' =>1,
                    'info'=>"修改成功"
                    );
            echo json_encode($arr);
           }else {
            $arr = array(
                    'error' =>0,
                    'info'=>"修改失败"
                    );
            echo json_encode($arr);
           } 

        }
    }

    //彻底删除
    public function delete()
    {
       // print_r($_POST);exit;
      if (IS_POST) {
          $id = I('post.id');

          //检测 角色是否有用户 关联 
          $sql = 'select * from auth_group_access where group_id='.$id;
          $access = M()->query($sql);
          if ($access) {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户组/角色删除失败,还有用户在使用此规则"
                  );
              echo json_encode($arr);
              exit;
          }

          $sql = 'delete from auth_group where id='.$id;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户组/角色删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户组/角色删除失败"
                  );
              echo json_encode($arr);
          }
      }  
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //检测 角色是否有用户 关联 
        $sql = 'select * from auth_group_access where group_id in '.'('.$ids.')';
        $access = M()->query($sql);
        if ($access) {
            $arr = array(
                'error' =>1,
                'info'=>"用户组/角色删除失败,还有用户在使用此规则"
                );
            echo json_encode($arr);
            exit;
        }


        $res = M('auth_group')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户组/角色删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户组/角色删除失败"
                  );
              echo json_encode($arr);
          }

      }

    }



}